import { defineStore } from 'pinia'

export const useStore = defineStore('use', {
	state: () => ({
		msg: 'Hello World!',
		count: 0,
    users: [
      { id: 1, name: 'kael' },
      { id: 2, name: 'yeahok' },
      { id: 3, name: 'zagss' }
    ],
	}),
	getters: {
		doubleCount: state => state.count * 2,
    doublePlusOne(): number {
      return this.doubleCount + 1
    },
		getUserById(state) {
      return userId => state.users.find(user => user.id === userId)
    },
    otherGetter(state) {
      const otherStore = useOtherStore()
      return state.msg + otherStore.msg
    }
	},
	actions: {
		increment() {
      this.count++
    },
    changeMsg(msg) {
      this.msg = msg
    },
    async registerUser(login, password) {
      try {
        this.userData = await api.post({ login, password })
        showTooltip(`Welcome back ${this.userData.name}!`)
      } catch (error) {
        showTooltip(error)
        return error
      }
    },
    async fetchUserPreferences() {
      const auth = useAuthStore()
      if (auth.isAuthenticated) {
        this.preferences = await fetchPreferences()
      } else {
        throw new Error('User must be authenticated')
      }
    },
	}
})